<template>
  <el-dialog
    :title="title"
    :visible.sync="openSheet"
    width="1100px"
    :destroy-on-close="true"
    :close-on-click-modal="false"
    :before-close="cancel"
  >
    <!-- 项目计划信息 -->
    <projectplanbase-info :project-plan-base-information="projectPlanBaseInformation" />
    <!-- 项目计划信息 -->
    <br>
    <el-table v-loading="loading" :data="projectPlanLawList" border>
      <el-table-column label="类型" prop="item" width="190px" />
      <el-table-column label="数值" prop="value" show-tooltip-when-overflow />
      <!-- <el-table-column :label="$t('operate')" align="center" width="180" class-name="fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="updateCalcDetail(scope.row)">{{ $t('modifyBtn') }}</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="deleteCalcDetail(scope.row)">{{ $t('deleteBtn') }}</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <el-button v-if="createFlag" type="text" icon="el-icon-plus" @click="handleAdd">{{ $t('addBtn') }}</el-button>
    <el-button v-else type="text" icon="el-icon-edit" @click="handleUpdate()">{{ $t('modifyBtn') }}</el-button>
    <el-button :disabled="createFlag" type="text" icon="el-icon-delete" @click="handleDelete()">{{ $t('deleteBtn') }}</el-button>

    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">{{ $t('cancel') }}</el-button>
    </div>

    <projectplancommon ref="projectplancommon" :section="'法律'" :fixed-items="legalItems" :form-data="formData" title="法律条款维护" @refreshList="getProjectPlanlawList" />
  </el-dialog>
</template>

<script>
import { getProjectPlanCommonListApi, deleteProjectPlanCommonApi } from '@/views/projectbase/common/projectplancommon.api'
import projectplanbaseInfo from '../../projectplanbase/components/projectplanbase-info.vue';
import projectplancommon from '@/views/projectbase/common/projectplancommon.vue';
import { getDropDownCodeTableListApi } from '@/api/common/common'

export default {
  components: { projectplanbaseInfo, projectplancommon },
  data() {
    return {
      title: undefined,
      openSheet: false,
      createFlag: true,
      loading: false,
      projectPlanId: undefined,
      projectPlanBaseInformation: {},
      projectPlanLawList: [],
      queryparams: {
        projectPlanFK: undefined,
        section: undefined
      },
      legalItems: [],
      formData: {}
    };
  },
  computed: {
  },
  methods: {
    async openSheetDialog(data) {
      this.openSheet = !this.openSheet;
      this.projectPlanBaseInformation = data;
      this.projectPlanId = data.id;
      this.initialize()
      this.getProjectPlanlawList()
    },
    cancel() {
      this.openSheet = !this.openSheet;
    },
    initialize() {
      this.handleLawOptions()
    },
    // 获取法律下拉选项
    async handleLawOptions() {
      var params = { 'model': 'CommonSectionModel', 'field': 'law_option' }
      var resp = await getDropDownCodeTableListApi(params)
      if (resp.success) {
        this.legalItems = resp.data;
      } else {
        this.targaMessageBox(this, resp.msg);
      }
    },
    // 获取项目计划基础信息表
    async getProjectPlanlawList() {
      this.loading = true
      this.queryparams.projectPlanFK = this.projectPlanId
      this.queryparams.section = '法律'
      var resp = await getProjectPlanCommonListApi({ ...this.queryparams })
      if (resp.success && resp.data.results.length > 0) {
        this.createFlag = false
        this.projectPlanLawList = resp.data.results
      } else {
        this.createFlag = true
        this.projectPlanLawList = []
      }
      this.loading = false
    },
    // 新增按钮
    handleAdd() {
      this.$refs.projectplancommon.openSheetDialog(this.projectPlanBaseInformation)
    },
    // 修改按钮
    handleUpdate() {
      // 如果是修改画面，则将传给子组件的参数，替换为当前项目计划ID下对应的法律信息
      this.formData = this.projectPlanLawList.reduce((acc, item) => {
        acc[item.item] = item.value;
        return acc;
      }, {});
      this.$refs.projectplancommon.openSheetDialog(this.projectPlanBaseInformation, this.formData)
    },
    // 删除按钮
    async handleDelete() {
      if (await this.ConfirmMessageBox(this.$t('projectPlanCustomerMaintainTitle'), this.$t('dataDeleteConfirm', { title: this.$t('customerCode'), value: '' }), 'warning') === 'cancel') {
        return
      }
      const resp = await deleteProjectPlanCommonApi({ projectPlanFK: this.projectPlanId });
      if (resp.success) {
        this.targaMessage(this.$t('deleteSuccess'));
        this.getProjectPlanlawList();
      } else {
        this.targaMessageBox(this, resp.msg);
      }
    }
  }
};
</script>

  <style scoped>
  </style>
